"use strict";
var Premium = Premium || {};

Premium.creative = {
    init: function() {
        /* START OF CUSTOM JS */

        Premium.communicator.init(document.body.id === "body_expanded" ? undefined : 4);

        switch (document.body.id) {
            case "body_top":
                // TOP ---------------------------------------------------------------

                Premium.creative.createVideoPlayerAppended = function(video, container) {
                    Premium.video.switchOnScroll(container, 3);
                    Premium.video.sync(video, 1);
                    var prom = video.play(); // Explicitly play the video
                    if (prom) {
                        prom.catch(function() {})
                    }
                    PremiumJpControls.callOnClickFullScreen(function(el) {
                        video.muted = false;
                        Premium.expand.expand("expanded.html", "width:100%;height:100%;")
                    })
                }

                break;

            case "body_left":
            case "body_right":

                Premium.creative.createVideoPlayerAppended = function(video, container) {
                    video.volume = 0;
                    Premium.video.switchOnScroll(container, 3);
                    Premium.video.sync(video, 1);
                    var prom = video.play(); // Explicitly play the video
                    if (prom) {
                        prom.catch(function() {})
                    }
                    PremiumJpControls.callOnClickFullScreen(function(el) {
                        video.muted = false;
                        Premium.expand.expand("expanded.html", "width:100%;height:100%;")
                    })
                }

                break;


            case "body_expanded":
                // EXPANDED ------------------------------------------------------------

                Premium.creative.createVideoPlayerAppended = function(video, container) {

                    // Regular sync
                    video.volume = 0;
                    Premium.video.sync(video, undefined, Premium.video.SyncType_Get);

                    // For smooth opening using a higher quailty video
                    let lag = 100,
                        isPlaying = false;
                    let playingHandler = function() {
                        isPlaying = true;
                        checkShow();
                    }
                    Premium.communicator.api.receiveMessage(function(mess) {
                        var obj = JSON.parse(mess);
                        if (obj.PremiumVideo && !document.body.classList.contains("fade-in")) {
                            if (!obj.playing) {
                                checkShow(true);
                            } else {
                                lag = Math.abs(obj.time - video.currentTime);
                                checkShow();
                            }
                        }
                    })
                    let checkShow = function(overrideShow) {
                        if (overrideShow || (isPlaying && (isNaN(lag) || lag <= 0))) {
                            video.removeEventListener("playing", playingHandler);
                            setTimeout(function() {
                                document.body.classList.add("fade-in");
                            }, 750)
                        }
                    }
                    video.addEventListener("playing", playingHandler);
                    video.play().catch(function() {})

                    // Safari fix
                    document.body.addEventListener("click", function(e) {
                        if (e.target.className && (e.target.className.indexOf("jp-controls-play") > -1 || e.target.className.indexOf("jp-controls-bigplay") > -1)) {
                            if (!video.paused) {
                                setTimeout(function() {
                                    video.play();
                                }, 1000)

                            }
                        }
                    });

                }
                Premium.creative.createVideoPlayerAppended(document.querySelector("VIDEO"));
                break;

        }
        /* END OF CUSTOM JS */
    }
}